---
title: "Replication File (Data Processing Survey 1): The Limits of Public Support for Fiscal Consolidation: Survey Evidence from Great Britain"
author: "Björn Bremer, Charlotte Cavaille, Lisanne de Blok, Catherine de Vries"
date: "February 3, 2026"
output:
  bookdown::html_document2:
    keep_tex: true
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
knitr::opts_chunk$set(message = FALSE)
knitr::opts_chunk$set(warning = FALSE)
```

```{r, include=FALSE}
rm(list = ls())
set.seed(1234567)

# List of packages needed
packages <- c("tidyverse"
)

# Function to check if packages are installed and install if needed
package_check <- function(pkg){
  if (!require(pkg, character.only = TRUE)) {
    install.packages(pkg, dependencies = TRUE)
    library(pkg, character.only = TRUE)
  }
}

# Apply function to each package
invisible(sapply(packages, package_check))

# Create directories to save data
if (!dir.exists("data")) {
  dir.create("data")
}
```

```{r, include=FALSE}
## clean the data
debt <- read.csv("Debt & Preferences - New version_February 8, 2022_07.44.csv", colClasses = c("character"), sep = ",")
debt <- debt[-c(1:2),] # remove unnecessary columns
debt <- debt[which(debt$treat!="9999"),]

# change the variable names
names(debt) <- gsub(x = names(debt), pattern = "\\.", replacement = "")  

# creating dummy for speeders
debt$duration <- as.numeric(debt$Durationinseconds)
debt$speeder <- ifelse(debt$duration<230 & debt$treat==1 |debt$duration<303 & debt$treat==2 |debt$duration<309 & debt$treat==3, 1, 0)
table(debt$speeder)

# create a measure for wheher respondents passed the sound check and the attention check and did not speed
debt$Finished <- as.numeric(debt$Finished)
debt$soundC <- as.numeric(debt$soundC)
debt$AC <- as.numeric(debt$AC)
debt$SC_passed <- ifelse(debt$soundC==3, 1, 0)
debt$AC_passed <- ifelse(debt$AC==8, 1, 0)
debt$check <- ifelse(debt$speeder==0 & debt$soundC==3 & debt$AC==8, 1, 0)
```

```{r controlvariables, include=FALSE}
## recode control variables

# age
debt$age <-2021-as.numeric(debt$yrbrn)
debt$age_rec <- ifelse(debt$age<35, "Up to 35", ifelse(debt$age>=35 & debt$age<51, "35-50", ifelse(debt$age>=51 & debt$age<65, "51-65", ifelse(debt$age>=65, "65+", NA))))

# gender
debt$gender_rec <- ifelse(debt$gender==1, "Female", ifelse(debt$gender==2 |debt$gender==3, "Male", NA))

#education
debt$educ <- as.numeric(debt$educ)
debt$edu_rec <- ifelse(debt$educ %in% c(1, 2, 3), "Low",        # Below A-Levels
                 ifelse(debt$educ == 4, "Middle",                 # A-Levels
                 ifelse(debt$educ %in% c(5, 6, 7, 8, 9), "High",  # Foundation Degree and above
                        NA)))

#employment
debt$emp <- as.numeric(debt$emp)
debt$emp_rec <- ifelse(debt$emp==10 | debt$emp==11 | debt$emp==16, "Employed or student",
                       ifelse(debt$emp==12 | debt$emp==13, "Unemployed", 
                              ifelse(debt$emp==15, "Retired", 
                                     ifelse(debt$emp==14 | debt$emp ==17 | debt$emp == 18, "Out of work force", NA))))

#income
debt$income <- debt$hhinc_w
debt$income <- ifelse(debt$income=="", debt$hhinc_m, debt$income)
debt$income <- ifelse(debt$income=="", debt$hhinc_y, debt$income)
debt$income <- ifelse(debt$income=="" | debt$income=="99" | debt$income=="88", NA, debt$income)
debt$income <- as.numeric(debt$income)
debt$income_cat <- ifelse(debt$income<=3, "Low",
                          ifelse(debt$income==4 |debt$income==5 |debt$income==6 |debt$income==7, "Middle",
                                 ifelse(debt$income>=8, "High", NA)))

# political variables / mediators vote
debt$vote_part1 <- as.numeric(debt$vote_part1)
debt$vote_part2 <- as.numeric(debt$vote_part2)

debt$party_1 <- ifelse(debt$vote_part1 == 1 | debt$vote_part1 == 7 | debt$vote_part1 == 8, "Right-wing",
                       ifelse(debt$vote_part1 == 2 | debt$vote_part1 == 4 | debt$vote_part1 == 5 | debt$vote_part1 == 6, "Left-wing",
                              ifelse(debt$vote_part1 == 3, "Lib Dems",
                                     ifelse(debt$vote_part1 == 9 | debt$vote_part1 == 88, "Other", NA))))

debt$party_2 <- ifelse(debt$vote_part2 == 1 | debt$vote_part2 == 5 | debt$vote_part2 == 7 | debt$vote_part2 == 8, "Right-wing",
                       ifelse(debt$vote_part2 == 2 | debt$vote_part2 == 4 | debt$vote_part2 == 6, "Left-wing",
                              ifelse(debt$vote_part2 == 3, "Lib Dems",
                                     ifelse(debt$vote_part2 == 9 | debt$vote_part2 == 88, "Other", NA))))
debt$party <- debt$party_1
debt$party <- ifelse(debt$party=="" | is.na(debt$party), debt$party_2, debt$party)

# partisanship variable with only Labour vs. Conservatives
debt$party_1_rec <- ifelse(debt$vote_part1 == 1, "Conservatives",
                       ifelse(debt$vote_part1 == 2, "Labour", NA))

debt$party_2_rec <- ifelse(debt$vote_part2 == 1, "Conservatives",
                       ifelse(debt$vote_part2 == 2, "Labour", NA))
debt$party_rec <- debt$party_1_rec
debt$party_rec <- ifelse(debt$party_rec=="" | is.na(debt$party_rec), debt$party_2_rec, debt$party_rec)

debt$party_1_rec2 <- ifelse(debt$vote_part1 == 1, "Conservatives",
                       ifelse(debt$vote_part1 == 2, "Labour",
                       ifelse(debt$vote_part1 == 3, "Lib Dem",
                       ifelse(debt$vote_part1 == 4, "SNP",
                       ifelse(debt$vote_part1 == 98 |debt$vote_part1 == 99, NA, "Other")))))

debt$party_2_rec2 <- ifelse(debt$vote_part2 == 1, "Conservatives",
                       ifelse(debt$vote_part2 == 2, "Labour",
                       ifelse(debt$vote_part2 == 3, "Lib Dem",
                       ifelse(debt$vote_part2 == 4, "SNP",
                       ifelse(debt$vote_part2 == 98 |debt$vote_part2 == 99, NA, "Other")))))
debt$party_rec2 <- debt$party_1_rec2
debt$party_rec2 <- ifelse(debt$party_rec2=="" | is.na(debt$party_rec2), debt$party_2_rec2, debt$party_rec2)
```

```{r outcomevariables, include=FALSE}
## recode outcome variables

# concern over debt
debt$debt_pre <- debt$MC_debt_V1_1 
debt$debt_pre <- ifelse(debt$debt_pre =="", debt$MC_debt_V1_2, debt$debt_pre)
debt$debt_pre <- ifelse(debt$debt_pre =="", debt$MC_debt_V2_3, debt$debt_pre)
debt$debt_pre <- ifelse(debt$debt_pre =="", debt$MC_debt_V2_4, debt$debt_pre)
debt$debt_pre <- as.numeric(debt$debt_pre)

debt$debt_pre_rec <- ifelse(debt$debt_pre==5, "Very serious",
                          ifelse(debt$debt_pre==4, "Serious",
                                 ifelse(debt$debt_pre==3, "Somewhat serious",
                                        ifelse(debt$debt_pre==2, "Not really serious",
                                               ifelse(debt$debt_pre==1, "Not serious", NA)))))

# concern over china
debt$china_pre <- debt$pb_china_V1_1 
debt$china_pre <- ifelse(debt$china_pre =="", debt$pb_china_V1_2, debt$china_pre)
debt$china_pre <- ifelse(debt$china_pre =="", debt$pb_china_V2_3, debt$china_pre)
debt$china_pre <- ifelse(debt$china_pre =="", debt$pb_china_V2_4, debt$china_pre)
debt$china_pre <- as.numeric(debt$china_pre)

# concern over climate
debt$climate_pre <- debt$pb_climate_V1_1 
debt$climate_pre <- ifelse(debt$climate_pre =="", debt$pb_climate_V1_2, debt$climate_pre)
debt$climate_pre <- ifelse(debt$climate_pre =="", debt$pb_climate_V2_3, debt$climate_pre)
debt$climate_pre <- ifelse(debt$climate_pre =="", debt$pb_climate_V2_4, debt$climate_pre)
debt$climate_pre <- as.numeric(debt$climate_pre)

# concern over immigration
debt$immi_pre <- debt$pb_immi_V1_1 
debt$immi_pre <- ifelse(debt$immi_pre =="", debt$pb_immi_V1_2, debt$immi_pre)
debt$immi_pre <- ifelse(debt$immi_pre =="", debt$pb_immi_V2_3, debt$immi_pre)
debt$immi_pre <- ifelse(debt$immi_pre =="", debt$pb_immi_V2_4, debt$immi_pre)
debt$immi_pre <- as.numeric(debt$immi_pre)

# debt after treatment (treatment)
debt$debt_post <- debt$MC_debt_V1_post
debt$debt_post <- ifelse(debt$debt_post =="", debt$MC_debt_V2_post, debt$debt_post)

# china after treatment (placebo)
debt$china_post <- debt$pb_china_V1_post 
debt$china_post <- ifelse(debt$china_post =="", debt$pb_china_v2_post, debt$china_post)

# policy index
debt$pol1_rec <- ifelse(debt$pol1=="", NA, debt$pol1)
debt$pol2_rec <- ifelse(debt$pol2=="", NA, debt$pol2)
debt$pol3_rec <- ifelse(debt$pol3=="", NA, debt$pol3)
debt$pol1_rec <- as.numeric(debt$pol1_rec)
debt$pol2_rec <- as.numeric(debt$pol2_rec)
debt$pol3_rec <- as.numeric(debt$pol3_rec)

debt$debt_index <- debt$pol1_rec + debt$pol2_rec + debt$pol3_rec

# debt vs. climate change
debt$debt_CC <- debt$prioD_CCa
debt$debt_CC <- ifelse(debt$debt_CC=="", debt$prioD_CCb, debt$debt_CC)
debt$debt_CC <- as.numeric(debt$debt_CC)

debt$debt_CC_rec <- ifelse(debt$debt_CC == 2, 1, 
                          ifelse(debt$debt_CC == 1, 0, NA))

# debt vs. immigration
debt$debt_immi <- debt$prioD_immia
debt$debt_immi <- ifelse(debt$debt_immi=="", debt$prioD_immib, debt$debt_immi)
debt$debt_immi <- as.numeric(debt$debt_immi)

debt$debt_immi_rec <- ifelse(debt$debt_immi == 2, 1, 
                          ifelse(debt$debt_immi == 1, 0, NA))

# debt vs. nhs
debt$debt_nhs <- debt$prioD_NHSa
debt$debt_nhs <- ifelse(debt$debt_nhs=="", debt$prioD_NHSb, debt$debt_nhs)
debt$debt_nhs <- as.numeric(debt$debt_nhs)

debt$debt_nhs_rec <- ifelse(debt$debt_nhs == 2, 1, 
                          ifelse(debt$debt_nhs == 1, 0, NA))

# debt allocation
debt$alloc_debt <- as.numeric(debt$alloc_1)
debt$alloc_cuts_edu <- as.numeric(debt$alloc_2)
debt$alloc_cuts_benefits <- as.numeric(debt$alloc_3)
debt$alloc_tax_income <- as.numeric(debt$alloc_4)
debt$alloc_tax_vat <- as.numeric(debt$alloc_5)

# expectations 
debt$exp_gdp <- ifelse(debt$gdpR_1 == "", NA, debt$gdpR_1)
debt$exp_infl <- ifelse(debt$inflR_13 == "", NA, debt$inflR_13)
debt$exp_tax <- ifelse(debt$taxR_17 == "", NA, debt$taxR_17)
debt$exp_pen <- ifelse(debt$returnR_13 == "", NA, debt$returnR_13)

debt$exp_nhs_costs <- debt$nhs1a
debt$exp_nhs_costs <- ifelse(debt$exp_nhs_costs=="", debt$nhs1b, debt$exp_nhs_costs)
debt$exp_nhs_costs <- ifelse(debt$exp_nhs_costs == "", NA, debt$exp_nhs_costs)

debt$exp_nhs_qual <- debt$nhs2a
debt$exp_nhs_qual <- ifelse(debt$exp_nhs_qual=="", debt$nhs2b, debt$exp_nhs_qual)
debt$exp_nhs_qual <- ifelse(debt$exp_nhs_qual == "", NA, debt$exp_nhs_qual)

cols <- c("exp_gdp","exp_infl", "exp_tax", "exp_pen")
debt[,cols] <- data.frame(apply(debt[cols], 2, as.numeric))

# certainty on debt
debt$cert_debt <- debt$cert_debt_V1_1 
debt$cert_debt <- ifelse(debt$cert_debt =="", debt$cert_debt_V1_2, debt$cert_debt)
debt$cert_debt <- ifelse(debt$cert_debt =="", debt$cert_debt_V2_3, debt$cert_debt)
debt$cert_debt <- ifelse(debt$cert_debt =="", debt$cert_debt_V2_4, debt$cert_debt)
debt$cert_debt <- as.numeric(debt$cert_debt)

debt$cert_debt_rec <- ifelse(debt$cert_debt==3, "Serious problem",
                             ifelse(debt$cert_debt==2, "Uncertain",
                                    ifelse(debt$cert_debt==1, "Not a serious problem", NA)))
debt$cert_debt_rec <- as.factor(debt$cert_debt_rec)

# having thought about debt
debt$think_debt <- debt$think_debt_V1_1 
debt$think_debt <- ifelse(debt$think_debt =="", debt$think_debt_V2_2, debt$think_debt)
debt$think_debt <- ifelse(debt$think_debt =="", debt$think_debt_V1_3, debt$think_debt)
debt$think_debt <- ifelse(debt$think_debt =="", debt$think_debt_V2_4, debt$think_debt)
debt$think_debt <- as.numeric(debt$think_debt)

debt$think_debt_rec <- ifelse(debt$think_debt==4, "A great deal",
                          ifelse(debt$think_debt==3, "Moderate amount",
                             ifelse(debt$think_debt==2, "A little",
                                    ifelse(debt$think_debt==1, "Never", NA))))
debt$think_debt_rec <- as.factor(debt$think_debt_rec)

# certainty on immigration
debt$cert_immi <- debt$cert_immi_V1_1 
debt$cert_immi <- ifelse(debt$cert_immi =="", debt$cert_immi_V1_2, debt$cert_immi)
debt$cert_immi <- ifelse(debt$cert_immi =="", debt$cert_immi_V2_3, debt$cert_immi)
debt$cert_immi <- ifelse(debt$cert_immi =="", debt$cert_immi_V2_4, debt$cert_immi)
debt$cert_immi <- as.numeric(debt$cert_immi)

debt$cert_immi_rec <- ifelse(debt$cert_immi==3, "Serious problem",
                             ifelse(debt$cert_immi==2, "Uncertain",
                                    ifelse(debt$cert_immi==1, "Not a serious problem", NA)))
debt$cert_immi_rec <- as.factor(debt$cert_immi_rec)

# certainty on climate
debt$cert_climate <- debt$cert_climate_V1_1 
debt$cert_climate <- ifelse(debt$cert_climate =="", debt$cert_climate_V1_2, debt$cert_climate)
debt$cert_climate <- ifelse(debt$cert_climate =="", debt$cert_climate_V2_3, debt$cert_climate)
debt$cert_climate <- ifelse(debt$cert_climate =="", debt$cert_climate_V2_4, debt$cert_climate)
debt$cert_climate <- as.numeric(debt$cert_climate)

debt$cert_climate_rec <- ifelse(debt$cert_climate==3, "Serious problem",
                             ifelse(debt$cert_climate==2, "Uncertain",
                                    ifelse(debt$cert_climate==1, "Not a serious problem", NA)))
debt$cert_climate_rec <- as.factor(debt$cert_climate_rec)

# certainty on china
debt$cert_china <- debt$cert_china_V1_1 
debt$cert_china <- ifelse(debt$cert_china =="", debt$cert_china_V1_2, debt$cert_china)
debt$cert_china <- ifelse(debt$cert_china =="", debt$cert_china_V2_3, debt$cert_china)
debt$cert_china <- ifelse(debt$cert_china =="", debt$cert_china_V2_4, debt$cert_china)
debt$cert_china <- as.numeric(debt$cert_china)

debt$cert_china_rec <- ifelse(debt$cert_china==3, "Serious problem",
                             ifelse(debt$cert_china==2, "Uncertain",
                                    ifelse(debt$cert_china==1, "Not a serious problem", NA)))
debt$cert_china_rec <- as.factor(debt$cert_china_rec)
```

```{r cleaning, include=FALSE}
# z standardization of variables
debt <- debt %>% 
  mutate(across(c(debt_post, china_post, debt_index, debt_nhs_rec, debt_CC_rec, debt_immi_rec, alloc_debt, alloc_cuts_edu, alloc_cuts_benefits, alloc_tax_income, alloc_tax_vat), ~ as.numeric(.))) %>%
  mutate(across(c(debt_post, china_post, debt_index, debt_nhs_rec, debt_CC_rec, debt_immi_rec, alloc_debt, alloc_cuts_edu, alloc_cuts_benefits, alloc_tax_income, alloc_tax_vat),
                ~ as.vector(scale(.)), 
                .names = "{.col}_z"))

# cleaning wave 1
debt_w1 <- debt
rm(debt)

debt_w1 <- debt_w1[which(duplicated(debt_w1$respid)==F),]
debt_w1$check_rec <- ifelse(debt_w1$speeder==0 & debt_w1$soundC==3 & debt_w1$AC==8 & debt_w1$Finished==1, 1, 0)
debt_w1 <- debt_w1[which(debt_w1$check_rec==1),]

debt_w1$treat <- as.factor(debt_w1$treat) # treatment as factor variable

debt_w1 <- debt_w1 %>% dplyr::select(respid, treat, debt_post, debt_post_z, block, debt_pre, edu_rec, party, debt_index, debt_index_z, pol1_rec, pol2_rec, pol3_rec, alloc_debt, alloc_debt_z, alloc_cuts_edu, alloc_cuts_edu_z, alloc_cuts_benefits, alloc_cuts_benefits_z, alloc_tax_income, alloc_tax_income_z, alloc_tax_vat, alloc_tax_vat_z, debt_CC_rec, debt_CC_rec_z, debt_immi_rec, debt_immi_rec_z, debt_nhs_rec, debt_nhs_rec_z, exp_gdp, exp_infl, exp_nhs_costs, exp_nhs_qual, exp_pen, exp_tax, cert_debt_rec, debt_CC, cert_china_rec, cert_climate_rec, cert_immi_rec, cert_climate, cert_china, cert_immi, cert_debt, climate_pre, immi_pre, china_pre, china_post, china_post_z, age, age_rec, gender_rec, income_cat, think_debt_rec, emp_rec, party_rec, MC_US, MC_S, rating1_S, rating2_S, rating1_US, rating2_US, income, party_rec2) 
debt_w1 <- debt_w1 %>% rename_all(paste0, "_w1")
debt_w1$respid <- debt_w1$respid_w1
debt_w1$treat <- debt_w1$treat_w1
```

```{r, echo=FALSE}
save(debt_w1, file = "data/data_processed_w1.RData")
```